﻿using System;
using System.Web.Mvc;
using CarsRegistrationService.Managers.Interfaces;
using CarsRegistrationService.Models.LightDataModel;
using CarsRegistrationService.Models.ViewModel;

namespace CarsRegistrationService.Controllers
{
    public class ExpertController : Controller
    {
        private IExpertManager expertManager;

        public ExpertController(IExpertManager expertManager)
        {
            this.expertManager = expertManager;
        }

        [HttpGet]
        public ActionResult LogIn()
        {
            if (Session["user"] != null)
            {
                Session.Remove("user");
            }
            var model = new LogInViewModel
                            {
                                Selection = "Login"
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult LogIn(LogInViewModel loginModel)
        {
            if (ModelState.IsValid)
            {
                if (!this.expertManager.IsUserDataCorrect(loginModel.Login, loginModel.Password))
                {
                    ModelState.AddModelError("Login", "Проверьте свой логин");
                    ModelState.AddModelError("Password", "Проверьте свой пароль");

                    var model = new LogInViewModel
                    {
                        Selection = "Login"
                    };
                    return View(model);
                }
                if (this.expertManager.Expert != null)
                {
                    Session["user"] = this.expertManager.Expert;
                    return RedirectToAction("RequestsPage");
                }
                return RedirectToAction("Index", "Admin");
            }
            return View();
        }

        [HttpGet]
        public ActionResult Registration()
        {
            var model = new RegistrationViewModel
                {
                    FirstName = "Eugene",
                    LastName = "Tikhonov",
                    Login = "TestUser",
                    MiddleName = "Sergeevich",
                    PassportNumber = "2005",
                    PassportSeries = "655753",
                    Password = "Password",
                    Selection = "Registration"
                };
            return View(model);
        }

        [HttpPost]
        public ActionResult Registration(RegistrationViewModel model)
        {
            if (ModelState.IsValid)
            {
                string errorMessage = this.expertManager.Registration(model.Login, model.Password,
                                                                      model.FirstName, model.LastName,
                                                                      model.MiddleName, model.PassportSeries,
                                                                      model.PassportNumber);
                if (errorMessage == null)
                {
                    Session["user"] = this.expertManager.Expert;
                    return RedirectToAction("RequestsPage");
                }
                ModelState.AddModelError("Login", errorMessage);
                return View(model);
            }
            return View();
        }

        [HttpGet]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult RequestsPage()
        {
            if (this.expertManager.Expert == null)
            {
                if (Session["user"] == null)
                {
                    return RedirectToAction("LogIn");
                }
                else
                {
                    this.expertManager.Expert = Session["user"] as LightExpert;
                }
            }
            var model = new RequestsViewModel();
            model.Login = this.expertManager.Expert.Login;
            model.Requests = this.expertManager.GetRequests();
            model.IsLoggedIn = true;
            model.Selection = "RequestList";
            return View(model);
        }

        [HttpGet]
        public ActionResult Create(Guid ID)
        {
            var model = new RequestProcessViewModel();
            model.Request = this.expertManager.GetLightRequest(ID);
            model.PersonPassport = new LightPersonPassport();
            model.VehiclePassport = new LightVehiclePassport();

            model.IsLoggedIn = true;

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Guid ID, LightPersonPassport personPassport, LightVehiclePassport vehiclePassport)
        {
            if (ModelState.IsValid)
            {
                this.expertManager.ProcessRequest(ID, personPassport, vehiclePassport);
                return RedirectToAction("RequestsPage");
            }
            else
            {
                var model = new RequestProcessViewModel();
                model.Request = this.expertManager.GetLightRequest(ID);
                model.PersonPassport = personPassport;
                model.VehiclePassport = vehiclePassport;
                model.IsLoggedIn = true;

                return View(model);
            }
        }

        [HttpGet]
        public ActionResult Delete(Guid ID)
        {
            this.expertManager.DeleteRequest(ID);
            return RedirectToAction("RequestsPage");
        }

        [HttpGet]
        public ActionResult GetRequests()
        {
            var requests = this.expertManager.GetRequests();
            return PartialView("RequestsTable", requests);
        }
    }
}
